layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 商品管理管理
     */
    var FruitsBasicGoods = {
        tableId: "fruitsBasicGoodsTable"
    };

    /**
     * 商品设置管理
     */
    var FruitsBasicGoodsInstall = {
        tableId: "fruitsBasicGoodsInstallTable"
    };

    /**
     * 初始化表格的列
     */
    FruitsBasicGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'details', sort: true, title: '描述'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FruitsBasicGoods.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(FruitsBasicGoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    FruitsBasicGoods.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/fruitsBasicGoods/add';
    };

    /**
     * 导出excel按钮
     */
    FruitsBasicGoods.exportExcel = function () {
        var checkRows = table.checkStatus(FruitsBasicGoods.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FruitsBasicGoods.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/fruitsBasicGoods/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FruitsBasicGoods.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/fruitsBasicGoods/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FruitsBasicGoods.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FruitsBasicGoods.tableId,
        url: Feng.ctxPath + '/fruitsBasicGoods/list',
        page: true,
        limit: 40,
        height: 400,
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: FruitsBasicGoods.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FruitsBasicGoods.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        FruitsBasicGoods.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FruitsBasicGoods.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + FruitsBasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FruitsBasicGoods.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FruitsBasicGoods.onDeleteItem(data);
        }
    });

    /**
     * 初始化表格的列
     */
    FruitsBasicGoodsInstall.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'goodsId', sort: true, title: '商品id', hide: true},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'grades', sort: true, title: '等级'},
            {field: 'caseWeightOne', sort: true, title: '每箱重量'},
            /*{field: 'caseSpecs', sort: true, title: '箱规'},*/
            {field: 'caseWeight', sort: true, title: '皮重'},
            {field: 'details', sort: true, title: '描述'},
            {align: 'center', toolbar: '#tableBarAddress', title: '操作'}
        ]];
    };

    // 单击行事件
    table.on('row(' + FruitsBasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + FruitsBasicGoods.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + FruitsBasicGoods.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        address(data);
    });

    // 单击行事件
    table.on('row(' + FruitsBasicGoodsInstall.tableId + ')', function (obj) {
		$('div[lay-id="' + FruitsBasicGoodsInstall.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + FruitsBasicGoodsInstall.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
    function address(data){
    	var goodsId;
    	if (data!=null) {
    		goodsId = data.id;
		} else {
			goodsId = 0;
		}
		
        // 渲染表格
        var tableResult2 = table.render({
            elem: '#' + FruitsBasicGoodsInstall.tableId,
            url: Feng.ctxPath + '/fruitsBasicGoodsInstall/list?goodsId='+goodsId,
            page: true,
            limit: 20,
            height: 380,
            toolbar: '#toolbarfromAddress',
            cellMinWidth: 100,
            cols: FruitsBasicGoodsInstall.initColumn()
        });

        // 添加按钮点击事件
        $('#btnAddAddress').click(function () {
            layer.open({
                type: 2,
                title: '添加商品等级',
                area: ['900px', '750px'],
                anim: 5,
                content: Feng.ctxPath + '/fruitsBasicGoodsInstall/add?goodsId='+goodsId,
                end: function () {
                	address(data);
                }
            });
        });

        /**
         * 点击编辑
         *
         * @param data 点击按钮时候的行数据
         */
        FruitsBasicGoodsInstall.openEditDlg = function (data2) {
        	console.log(data2);
            layer.open({
                type: 2,
                title: '修改商品等级',
                area: ['900px', '750px'],
                anim: 5,
                content: Feng.ctxPath + '/fruitsBasicGoodsInstall/edit?id='+data2.id,
                end: function () {
                	address(data);
                }
            });
        };

        /**
         * 点击删除
         *
         * @param data 点击按钮时候的行数据
         */
        FruitsBasicGoodsInstall.onDeleteItem = function (data2) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/fFruitsBasicGoodsInstall/delete", function (data2) {
                    Feng.success("删除成功!");
                	address(data);
                }, function (data) {
                    Feng.error("删除失败!" + data2.responseJSON.message + "!");
                });
                ajax.set("id", data2.id);
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        };
        
        // 工具条点击事件
        table.on('tool(' + FruitsBasicGoodsInstall.tableId + ')', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'editAddress') {
                FruitsBasicGoodsInstall.openEditDlg(data);
            } else if (layEvent === 'deleteAddress') {
                FruitsBasicGoodsInstall.onDeleteItem(data);
            }
        });
    }
});
